<?php
/**
 * [LupMisNotAllowedSync]
 * 本代码为系统自动生成代码，请根据自己业务进行修改;
 * 生成时间 2022-02-15 21:16:18;
 * 版权所有 2020-2022 lizhongwen，并保留所有权利;
 * 说明: 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用；不允许对程序代码以任何形式任何目的的再发布;
 * 作者: 中文Lee;
 * 作者主页: http://www.lizhongwen.com;
 * 邮箱: 360811363@qq.com;
 * QQ: 360811363;
 */
namespace modules\sys\service\impl;

use application\core\Lib;
use application\core\Request;
use application\core\Result;
use application\core\SnowFlake;
use application\core\Upload;
use application\filter\SysFilter;
use modules\sys\mapper\FilesMapper;
use modules\sys\service\UploadService;

class UploadServiceImpl implements UploadService
{

    function uploadFile(): array
    {
        $upload = new Upload();
        $path = Request::post("path");
        $thumb = Request::post("thumb");
        $remarks = Request::post("remarks","后台上传");
        if (!empty($path)) {
            $path = $path . "/";
        } else {
            $path = date("Ym") . "/" . date("d") . "/";
        }
        $uploadPath = '/uploads/';
        $upload->set("path", APP_PATH . WEB_ROOT_PATH . $uploadPath . $path);
        $upload->set("maxSize", UPLOAD_MAX_SIZE * 1024 * 1024);
        $upload->set("allowType", explode(",", UPLOAD_ALLOW_TYPE));
        $filePath = "";
        $serverPath = "";
        $fileName = "";
        $fileUrl = "";
        $originName = "";
        $suffix = "";
        $thumbRes = [];
        if ($upload->upload("file")) {
            $fileName = $upload->getFileName();
            $filePath = $uploadPath . $path . $fileName;
            $serverPath = APP_PATH . WEB_ROOT_PATH . $uploadPath . $path . $fileName;
            $serverPath = str_replace("//", "/", $serverPath);
            $serverPath = str_replace("\\", "/", $serverPath);
            $fileUrl = SERVICE_NAME . $filePath;
            $originName = $upload->getOriginName();
            $path_info = pathinfo($fileUrl);
            $suffix = strtolower($path_info['extension']);
            $thumbType = ['jpg', 'png', 'jpeg', 'bmp', 'gif'];
            if (!empty($thumb) && in_array($suffix, $thumbType)) {
                $thumbArray = explode("|", $thumb);
                foreach ($thumbArray as $thumbV) {
                    $_thumb = explode(",", $thumbV);
                    $_width = $_thumb[0];
                    $_height = $_thumb[1];
                    $_q = $_thumb[2] ?? 100;
                    if ($_width && $_height) {
                        $thumbFileUrl = SERVICE_NAME . $filePath . "-" . $_width . "_" . $_height . "_" . $_q . "." . $suffix;
                        $oldSavePath = APP_PATH . WEB_ROOT_PATH . $uploadPath . $path . $fileName;
                        $newSavePath = $oldSavePath . "-" . $_width . "_" . $_height . "_" . $_q . "." . $suffix;
                        Lib::setImgThumb($serverPath, $newSavePath, $_width, $_height, 2, $_q);
                        $thumbRes[] = $thumbFileUrl;
                    }

                }
            }
        }
        $data = [
            'suffix' => $suffix,
            'ori_name' => $originName,
            'file_name' => $fileName,
            'file_path' => $filePath,
            'file_url' => $fileUrl,
            'file_size'=> Lib::fmtMoney2(filesize($serverPath)/1024)."kb",
            'server_path' => $serverPath,
            'thumb' => $thumbRes
        ];
        //添加到oss
        $addData = [
            'id'=>SnowFlake::instance()->nextId(),
            'ori_name'=>$originName,
            'file_path'=>$filePath,
            'suffix'=>$suffix,
            'file_name'=>$fileName,
            'file_url'=>$fileUrl,
            'server_path'=>$serverPath,
            'thumb'=>implode(',',$thumbRes),
            'file_size'=>Lib::fmtMoney2(filesize($serverPath)/1024)."kb",
            'upload_time'=>date("Y-m-d H:i:s"),
            'account'=>SysFilter::$accountName,
            'remarks'=>$remarks
        ];
        FilesMapper::add($addData);
        return Result::success(true,$data, "上传成功");
    }
}